﻿/*--------------------------------------------------------------------------------
 * Adaptive Intelligence - Extended Code Document Object Model
 * Version 2.10
 * 
 * AI Project Class
 *      Represents and manages a project and its contents.
 *------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------
 * System Imports
 *------------------------------------------------------------------------------*/
using System;
using System.Text;
using System.Xml;
using System.ComponentModel;
using Adaptive.Foundation;
using Adaptive.Foundation.Collections;

namespace Adaptive.Foundation.CodeDom
{
    /// <summary>
    /// Represents and manages a project and its contents.
    /// </summary>
    public class AiProject : AiObjectBase, IXmlStorable
    {
        /*--------------------------------------------------------------------------------
         * Public Event Definitions
         *------------------------------------------------------------------------------*/
        #region Public Event Definitions
        #endregion

        /*--------------------------------------------------------------------------------
         * Private Member Declarations
         *------------------------------------------------------------------------------*/
        #region Private Member Declarations
        /// <summary>
        /// Description of the folder.
        /// </summary>
//        private string _description;
        /// <summary>
        /// Folders within the project.
        /// </summary>
        private AiFolderCollection _folders;
        /// <summary>
        /// Type of project being represented.
        /// </summary>
        private AiProjectTypes _projectType = AiProjectTypes.None;
        /// <summary>
        /// Language in which the project is to be rendered.
        /// </summary>
        private VisualStudioLanguages _language = VisualStudioLanguages.None;
        #endregion

        /*--------------------------------------------------------------------------------
         * Constructor / Dispose / Destructor Methods
         *------------------------------------------------------------------------------*/
        #region Constructor / Dispose / Destructor Methods
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <remarks>
        /// This is the default constructor.
        /// </remarks>
        public AiProject()
            : base()
        {
            _folders = new AiFolderCollection();
        }
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <param name="projectName">
        /// The name of the project.
        /// </param>
        public AiProject(string projectName)
            : base()
        {
            Name = projectName;
            _folders = new AiFolderCollection();
        }
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <param name="projectName">
        /// The name of the project.
        /// </param>
        /// <param name="projectType">
        /// An <see cref="AiProjectTypes"/> enumerated value indicating the project type.
        /// </param>
        /// <param name="language">
        /// A <see cref="VisualStudioLanguages"/> enumerated value indicating the language of the project.
        /// </param>
        public AiProject(string projectName, AiProjectTypes projectType, VisualStudioLanguages language)
            : base()
        {
            Name = projectName;
            _projectType = projectType;
            _language = language;
            _folders = new AiFolderCollection();
        }
        /// <summary>
        /// Deallocates internally allocated objects.
        /// </summary>
        /// <param name="disposing">
        /// A value indicating whether the object is being disposed.
        /// </param>
        protected override void Dispose(bool disposing)
        {
            if ((!Disposed) && (disposing))
            {
                if (_folders != null)
                    _folders.Dispose();
            }
            _folders = null;
            base.Dispose(disposing);
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Properties
         *------------------------------------------------------------------------------*/
        #region Public Properties
        /// <summary>
        /// Gets the reference to the list of sub-folders in the project.
        /// </summary>
        /// <value>
        /// An <see cref="AiFolderCollection"/> instance.
        /// </value>
        public AiFolderCollection Folders
        {
            get { return _folders; }
        }
        /// <summary>
        /// Gets or sets the project type.
        /// </summary>
        /// <value>
        /// An <see cref="AiProjectTypes"/> value indicating the project type.
        /// </value>
        [Browsable(true), DefaultValue(AiProjectTypes.None), Description("The type of project.")]
        public AiProjectTypes ProjectType 
        {
            get { return _projectType; }
            set { _projectType = value; }
        }
        /// <summary>
        /// Gets or sets the programming language for the project.
        /// </summary>
        /// <value>
        /// A <see cref="VisualStudioLanguages"/> enumerated value indicating the language the 
        /// project may be rendered in.
        /// </value>
        [Browsable(true), DefaultValue(false), Description("The programming language for the project.")]
        public VisualStudioLanguages Language
        {
            get { return _language; }
            set { _language = value; }
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Protected Methods / Event Methods
         *------------------------------------------------------------------------------*/
        #region Protected Methods / Event Methods
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Methods / Functions
         *------------------------------------------------------------------------------*/
        #region Public Methods / Functions
        /// <summary>
        /// Clears the contents of the object instance without disposing of the instance.
        /// </summary>
        public override void Clear()
        {
            _folders.Clear();
            _projectType = AiProjectTypes.None;
            _language = VisualStudioLanguages.None;
        }
        /// <summary>
        /// Creates the basic folders for an empty project.
        /// </summary>
        public void CreateDefaultFolderStructure()
        {
            AiFolder newFolder = null;

            newFolder = new AiFolder("Classes");
            newFolder.IndicatesNamespace = false;
            newFolder.Description = "Contains the class definitions.";
            _folders.Add(newFolder);

            newFolder = new AiFolder("Enumerations");
            newFolder.IndicatesNamespace = false;
            newFolder.Description = "Contains the common enumeration definitions.";
            _folders.Add(newFolder);

            newFolder = new AiFolder("Interfaces");
            newFolder.IndicatesNamespace = false;
            newFolder.Description = "Contains the common interface definitions.";
            _folders.Add(newFolder);
        }
        /// <summary>
        /// Attempts to save the specific object instance to an open stream.
        /// </summary>
        /// <param name="writer">
        /// An open <see cref="XmlWriter"/> instance used to write the content
        /// to the open stream.
        /// </param>
        /// <returns>
        /// <b>true</b> if the operation is successful; otherwise, returns <b>false</b>.
        /// </returns>
        public bool Save(XmlWriter writer)
        {
            bool returnValue = true;

            try
            {
                writer.WriteStartElement("AiProject");
                writer.WriteAttributeString("name", Name);
                writer.WriteAttributeString("projectType", ((int)_projectType).ToString());
                writer.WriteAttributeString("language", ((int)_language).ToString());
                writer.WriteStartElement("folders");
                _folders.MoveFirst();
            }
            catch
            {
                returnValue = false;
            }

            while ((!_folders.Eof) && (returnValue))
            {
                returnValue = _folders.Value.Save(writer);
                _folders.MoveNext();
            }

            if (returnValue)
            {
                try
                {
                    writer.WriteEndElement();
                    writer.WriteEndElement();
                }
                catch
                {
                    returnValue = false;
                }
            }
            return returnValue;
        }
        /// <summary>
        /// Attempts to load the specific object content from an open stream.
        /// </summary>
        /// <param name="reader">
        /// An open <see cref="XmlReader"/> instance used to reader content
        /// from the open stream.
        /// </param>
        /// <returns>
        /// <b>true</b> if the operation is successful; otherwise, returns <b>false</b>.
        /// </returns>
        public bool Load(XmlReader reader)
        {
            return false;
        }
        /// <summary>
        /// Returns an XML representation of the current object instance.
        /// </summary>
        /// <returns>
        /// XML Code representing the current object instance.
        /// </returns>
        public string ToXml()
        {
            StringBuilder builder = null;

            builder = new StringBuilder();
            builder.Append("<AiProject name=\"" + Name + "\" projectType=\"" + ((int)_projectType).ToString() +
                         " language=" + ((int)_language).ToString() + "\">\r\n");

            //Add projects.
            builder.Append("\t<folders>\r\n");
            _folders.MoveFirst();
            while (!_folders.Eof)
            {
                builder.Append("\t\t" + _folders.Value.ToXml());
                _folders.MoveNext();
            }
            builder.Append("\t</folders>\r\n");
            builder.Append("</AiProject>\r\n");

            return builder.ToString();
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Private Event Handlers
         *------------------------------------------------------------------------------*/
        #region Private Event Handlers
        #endregion

        /*--------------------------------------------------------------------------------
         * Private Methods / Functions
         *------------------------------------------------------------------------------*/
        #region Private Methods / Functions
        #endregion

    }
}
